package com.gitee.advice;

import com.gitee.exception.*;
import com.gitee.vo.ResponseVo;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseVo<String> handler(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        return ResponseVo.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
    }

    @ExceptionHandler(LoginException.class)
    public ResponseVo<String> handler(LoginException e) {
        return ResponseVo.error(401, e.getMessage());
    }

    @ExceptionHandler(UserException.class)
    public ResponseVo<String> handler(UserException e) {
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(OrderException.class)
    public ResponseVo<String> handler(OrderException e) {
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(FoodException.class)
    public ResponseVo<String> handler(FoodException e) {
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(CookbookException.class)
    public ResponseVo<String> handler(CookbookException e) {
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(CommentException.class)
    public ResponseVo<String> handler(CommentException e) {
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(AdminException.class)
    public ResponseVo<String> handler(AdminException e) {
        return ResponseVo.error(e.getMessage());
    }

    @ExceptionHandler(CartException.class)
    public ResponseVo<String> handler(CartException e) {
        return ResponseVo.error(e.getMessage());
    }

}
